<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加管理员</title>
    <link rel="stylesheet" th:href="@{/webjars/layui/2.5.7/css/layui.css}">
</head>
<body>
<div class="layui-layout layui-layout-admin">

    <div th:replace="layout::header"></div>
    <div th:replace="layout::side"></div>

    <div class="layui-body">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>添加管理员</legend>
        </fieldset>

        <!--表单-->
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">管理员名</label>
                <div class="layui-input-block">
                    <input type="text" id="adminName" name="adminName" required  lay-verify="required" placeholder="请输入管理员名" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">管理员密码</label>
                <div class="layui-input-block">
                    <input type="password" id="password" name="password" required  lay-verify="required" placeholder="请输入管理员密码" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">管理员角色</label>
                <div class="layui-input-block">
                    <input type="text" id="role" name="role" required  lay-verify="required" placeholder="请输入管理员角色" autocomplete="off" class="layui-input">
                </div>
            </div>
        </form>
        <div class="layui">
            <div class="layui-input-block">
                <button class="layui-btn" onclick="input()">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </div>
</div>

<script th:src="@{/webjars/layui/2.5.7/layui.js}"></script>
<script th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>

<script>
    function input() {
        let serverUrl = 'http://localhost:8080/'
        let message = {}
        message.adminName = document.getElementById("adminName").value
        message.password = document.getElementById("password").value
        message.role = document.getElementById("role").value
        alert(JSON.stringify(message))
        $.ajax({
            //请求的服务器地址
            url: serverUrl + "admin/addAdmin",
            method: "POST",
            //请求头部
            headers: {
                //告诉服务端解析的是json数据
                "Content-Type": "application/json"
            },
            data: JSON.stringify(message),
            //请求成功响应后的回调函数
            success: function (date) {
                var res = date;
                //data是成功回调后后端返回的数据
                alert("res.data.code")
                console.log(res)
                console.log(res.data)
                if (res.code === 200) {
                    alert(res.message)
                } else {
                    alert(res.message)
                }
            },
            error: function(res) {
            }
        });
    }
</script>
</body>
</html>